home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / XVThumbImagePlugin.py < prev   
Encoding:
Text File  |  2000-06-23  |  1.5 KB  |  66 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: XVThumbImagePlugin.py,v 1.1.1.1 1999/01/13 09:40:16 sjoerd Exp $
  4. #
  5. # XV Thumbnail file handler by Charles E. "Gene" Cash
  6. # (gcash@magicnet.net)
  7. # see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,
  8. # available from ftp://ftp.cis.upenn.edu/pub/xv/
  9. #
  10. # history:
  11. # 98-08-15 cec    created (b/w only)
  12. # 98-12-09 cec    added color palette
  13. # 98-12-28 fl    added to PIL (with only a few very minor modifications)
  14. #
  15. # To do:
  16. # FIXME: make save work (this requires quantization support)
  17. #
  18.  
  19. __version__ = "0.1"
  20.  
  21. import string
  22. import Image, ImageFile, ImagePalette
  23.  
  24. # standard color palette for thumbnails (RGB332)
  25. PALETTE = ""
  26. for r in range(8):
  27.     for g in range(8):
  28.     for b in range(4):
  29.         PALETTE = PALETTE + (chr((r*255)/7)+chr((g*255)/7)+chr((b*255)/3))
  30.  
  31. class XVThumbImageFile(ImageFile.ImageFile):
  32.  
  33.     format = "XVThumb"
  34.     format_description = "XV thumbnail image"
  35.  
  36.     def _open(self):
  37.  
  38.     # check magic
  39.     s = self.fp.read(6)
  40.     if s != "P7 332":
  41.         raise SyntaxError, "not an XV thumbnail file"
  42.  
  43.     # skip info comments
  44.     while 1:
  45.         s = string.strip(self.fp.readline())
  46.         if s == "#END_OF_COMMENTS":
  47.         break
  48.  
  49.     # read header line
  50.     s = string.split(self.fp.readline())
  51.  
  52.     self.mode = "P"
  53.     self.size = int(s[0]), int(s[1])
  54.  
  55.     self.palette = ImagePalette.raw("RGB", PALETTE)
  56.  
  57.     self.tile = [
  58.         ("raw", (0, 0)+self.size,
  59.          self.fp.tell(), (self.mode, 0, 1)
  60.          )]
  61.  
  62. # --------------------------------------------------------------------
  63.  
  64. Image.register_open("XVThumb", XVThumbImageFile)
  65.